package com.lzy.imagepicker.ui;

import android.annotation.TargetApi;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageButton;

import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.R;
import com.lzy.imagepicker.adapter.ImageAndVideoPageAdapter;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.util.NavigationBarChangeListener;
import com.lzy.imagepicker.util.Utils;
import com.lzy.imagepicker.view.SystemBarTintManager;
import com.lzy.imagepicker.view.ViewPagerFixed;

import java.util.ArrayList;




/**
 * ================================================
 * 作    者：jeasonlzy（廖子尧），ikkong （ikkong@163.com）
 * 版    本：1.0
 * 创建日期：2016/5/19
 * 描    述：
 * 修订历史：预览已经选择的图片，并可以删除, 感谢 ikkong 的提交
 * ================================================
 */
public class ImageAndVideoPreviewSeeActivity extends ImageBaseActivity {
    protected SystemBarTintManager tintManager;
    protected ImagePicker imagePicker;
    protected ArrayList<ImageItem> mImageItems      = new ArrayList<>();      //跳转进ImagePreviewFragment的图片文件夹
    protected int                  mCurrentPosition = 0;              //跳转进ImagePreviewFragment时的序号，第几个图片
    protected ImageAndVideoPageAdapter mAdapter;


    ViewPagerFixed mViewpager;
    FrameLayout mContent;


    //    @BindView(R.id.tv_des)
    //    TextView mTvDes;  //显示当前图片的位置  例如  5/31
    //    @BindView(R.id.btn_ok)
    //    Button mBtnOk;
    //
    //    @BindView(R.id.top_bar)
    //    RelativeLayout mTopBar;
    //
    //    @BindView(R.id.btn_del)
    //    AppCompatImageView mDelBtn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_and_video_preview);

        initData();
    }



    protected void initData() {
         mViewpager = findViewById(R.id.viewpager);
        mContent = findViewById(R.id.content);
        ImageButton ib = findViewById(R.id.ib_back);
        ib.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        //获取数据
//        boolean isDelete = getIntent().getBooleanExtra(imagePicker.EXTRA_IS_DELETE, false);
        mCurrentPosition = getIntent().getIntExtra(ImagePicker.EXTRA_SELECTED_IMAGE_POSITION, 0);
        ArrayList<ImageItem> list = getIntent().getParcelableArrayListExtra(ImagePicker.EXTRA_IMAGE_ITEMS);

        if (list != null && list.size() > 0) {
            mImageItems.addAll(list);
        }

        imagePicker = ImagePicker.getInstance();

//        if (isDelete) {
//            mDelBtn.setVisibility(View.VISIBLE);
//        } else {
//            mDelBtn.setVisibility(View.GONE);
//        }
//
//
//        mBtnOk.setVisibility(View.GONE);
//        mTvDes.setText(getString(R.string.ip_preview_image_count, mCurrentPosition + 1, mImageItems.size()));

        //状态相关设置
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
        }
        tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setStatusBarTintResource(Color.TRANSPARENT);//通知栏所需颜色
        
        //因为状态栏透明后，布局整体会上移，所以给头部加上状态栏的margin值，保证头部不会被覆盖
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mContent.getLayoutParams();
            params.topMargin = Utils.getStatusHeight(this);
            mContent.setLayoutParams(params);
        }
        NavigationBarChangeListener.with(this, NavigationBarChangeListener.ORIENTATION_HORIZONTAL).setListener(new NavigationBarChangeListener
                .OnSoftInputStateChangeListener() {
            @Override
            public void onNavigationBarShow(int orientation, int height) {
                mContent.setPadding(0, 0, height, 0);
            }

            @Override
            public void onNavigationBarHide(int orientation) {
                mContent.setPadding(0, 0, 0, 0);
            }
        });


        initPagerView();

    }



    private void initPagerView() {
        mAdapter = new ImageAndVideoPageAdapter(this, mImageItems);

        //单击显示隐藏标题栏
//        mAdapter.setPhotoViewClickListener(new ImageAndVideoPageAdapter.PhotoViewClickListener() {
//            @Override
//            public void OnPhotoTapListener(View view, float v, float v1) {
//                if (mTopBar.getVisibility() == View.VISIBLE) {
//                    mTopBar.setAnimation(AnimationUtils.loadAnimation(ImageAndVideoPreviewSeeActivity.this, R.anim.image_picker_top_out));
//                    mTopBar.setVisibility(View.GONE);
//                    tintManager.setStatusBarTintResource(Color.TRANSPARENT);//通知栏所需颜色
//                } else {
//                    mTopBar.setAnimation(AnimationUtils.loadAnimation(ImageAndVideoPreviewSeeActivity.this, R.anim.image_picker_top_in));
//                    mTopBar.setVisibility(View.VISIBLE);
//                    tintManager.setStatusBarTintResource(R.color.ip_color_primary_dark);//通知栏所需颜色
//                }
//            }
//        });

        mViewpager.setAdapter(mAdapter);
        mViewpager.setCurrentItem(mCurrentPosition, false);


        //滑动ViewPager的时候，根据外界的数据改变当前的选中状态和当前的图片的位置描述文本
        mViewpager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                mCurrentPosition = position;
//                mTvDes.setText(getString(R.string.ip_preview_image_count, mCurrentPosition + 1, mImageItems.size()));
            }
        });

    }


    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        ImagePicker.getInstance().restoreInstanceState(savedInstanceState);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        ImagePicker.getInstance().saveInstanceState(outState);
    }

    @Override
    protected void onDestroy() {
        if (mAdapter != null && mAdapter.getVideoPlayer() != null) {
            mAdapter.getVideoPlayer().onVideoPause();
        }
        super.onDestroy();
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        //带回最新数据
        intent.putExtra(ImagePicker.EXTRA_IMAGE_ITEMS, mImageItems);
        setResult(ImagePicker.RESULT_CODE_BACK, intent);
        finish();
        super.onBackPressed();
    }

//    @OnClick(R.id.btn_back)
//    public void onMBtnBackClicked() {
//        onBackPressed();
//    }
//
//    @OnClick(R.id.btn_del)
//    public void onMBtnDelClicked() {
//        showDeleteDialog();
//    }


    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }


//    /**
//     * 是否删除此张图片
//     */
//    private void showDeleteDialog() {
//        AlertDialog.Builder builder = new AlertDialog.Builder(this);
//        builder.setTitle("提示");
//        builder.setMessage("要删除这张照片吗？");
//        builder.setNegativeButton("取消", null);
//        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                //移除当前图片刷新界面
//                mImageItems.remove(mCurrentPosition);
//                if (mImageItems.size() > 0) {
//                    mAdapter.setData(mImageItems);
//                    mAdapter.notifyDataSetChanged();
//                    mTvDes.setText(getString(R.string.ip_preview_image_count, mCurrentPosition + 1, mImageItems.size()));
//                } else {
//                    onBackPressed();
//                }
//            }
//        });
//        builder.show();
//    }

}